package com.some.jobdescapp.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.randy.recyclerviews.adapter.ExpandableListAdapter;
import com.randy.recyclerviews.adapter.UniversalAdapter;
import com.randy.recyclerviews.adapter.eventhook.OnClickEventHook;
import com.some.jobdescapp.R;
import com.some.jobdescapp.bean.ChildEntity;
import com.some.jobdescapp.bean.GroupEntity;
import com.some.jobdescapp.model.ContentModel;
import com.some.jobdescapp.model.LineItemDecoration;
import com.some.jobdescapp.model.TitleModel;
import com.some.jobdescapp.presenter.HomePresenter;
import com.some.jobdescapp.view.IHomeView;

public class HomeActivity extends BaseActivity implements IHomeView{
    private RecyclerView recyclerView;
    private HomePresenter presenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        recyclerView = findViewById(R.id.rv_list);
        presenter = new HomePresenter(this);

        presenter.initDemoData();

    }

    @Override
    public void bindAdapter(final ExpandableListAdapter adapter) {
//        adapter.setOnItemClickListener(new UniversalAdapter.OnItemClickListener() {
//            @Override
//            public void onClick(@NonNull View itemView, @NonNull UniversalAdapter.ViewHolder viewHolder, int position, @NonNull UniversalAdapter.AbstractModel<?> model) {
//                if (model instanceof TitleModel) {
//                    ExpandableList expandableList = ((TitleModel) model).getExpandableList();
//                    expandableList.setCollapsed(!expandableList.isCollapsed());
//                    adapter.notifyDataChanged(expandableList);
//                }
//            }
//        });

        adapter.addEventHook(new OnClickEventHook<UniversalAdapter.ViewHolder>(UniversalAdapter.ViewHolder.class) {
            @Override
            public void onClick(@NonNull View view, @NonNull UniversalAdapter.ViewHolder viewHolder, int position, @NonNull UniversalAdapter.AbstractModel rawModel) {
                if (rawModel instanceof TitleModel) {
                    GroupEntity groupEntity = ((TitleModel) rawModel).getGroupEntity();
                    groupEntity.setCollapsed(!groupEntity.isCollapsed());
                    ((TitleModel) rawModel).getExpandableList().setCollapsed(groupEntity.isCollapsed());
                    adapter.notifyDataChanged(null);
                } if (rawModel instanceof ContentModel) {
                    ChildEntity childEntity = ((ContentModel) rawModel).getChildEntity();
                    Intent intent = new Intent(HomeActivity.this, JobDescActivity.class);
                    intent.putExtra("data", childEntity.getJobDesc());
                    startActivity(intent);
                }
            }

            @Nullable
            @Override
            public View onBind(@NonNull UniversalAdapter.ViewHolder viewHolder) {
                return viewHolder.itemView;
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new LineItemDecoration(HomeActivity.this, LinearLayoutManager.VERTICAL, dpToPx(HomeActivity.this, 0.5f), Color.parseColor("#CCCCCC")));
        recyclerView.setItemAnimator(null);
        recyclerView.setAdapter(adapter);
    }

    public static int dpToPx(Context context, float dpValue) {
        final float scale =context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
